import {
  toast,
  distance
} from '../../utils/util' // personal/myPubLish/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switchBar: true,
    tabIndex: 0,
    rent: [],
    enterprise: [],
    sell: [],
    maintain: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPublishRent()
    this.getPublishSell()
    this.getPublishMaintian()
  },
  getPublishRent() {
    const data = {
      inqurystate: 1,
      equtype: '',
      lon: 0,
      lat: 0,
      userid: wx.getStorageSync('userInfo').userid
    }
    app.$api.index.rentFind({
        ...data
      })
      .then((res) => {
        const data = res.data.data
        console.log(data)
        const info = data.map((item) => {
          return this.calcDistance(item)
        })
        console.log(info)
        this.setData({
          rent: info
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  getPublishSell() {
    const result = []
    const data2 = {
      inqurystate: 1,
      entscope: '',
      lon: 0,
      lat: 0,
      userid: wx.getStorageSync('userInfo').userid
    }
    app.$api.index.saleFind({
        ...data2
      })
      .then((res) => {
        const data = res.data.data
        const info = data.map((item) => {
          return this.calcDistance(item)
        })
        this.setData({
          sell: info
        })
        this.data.sell.map((item) => {
          app.$api.common.commonFindName({
              code: item.enttype
            })
            .then((s) => {
              const data = s.data
              result.push(data.name)
              this.setData({
                enterprise: result
              })
              console.log(this.data.enterprise)
            }).catch((e) => {
              toast('请稍后再试', 1000)
            })
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  getPublishMaintian() {
    const data3 = {
      inqurystate: 1,
      maintainscope: '',
      lon: 0,
      lat: 0,
      userid: wx.getStorageSync('userInfo').userid
    }
    app.$api.index.maintainFind({
        ...data3
      })
      .then((res) => {
        const data = res.data.data
        const info = data.map((item) => {
          return this.calcDistance(item)
        })
        this.setData({
          maintain: info
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  calcDistance(item) {
    const locationA = {
      lat: wx.getStorageSync('location').lat,
      lng: wx.getStorageSync('location').lng
    }
    const locationB = {
      lat: Number(item.lat),
      lng: Number(item.lon)
    }
    const distanceValue = distance(locationA, locationB)
    item = Object.assign({}, item, {
      distance: distanceValue
    })
    return item
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switch1Change(e) {
    console.log('switch1 发生 change 事件，携带值为', e.detail.value)
  },
  switch2Change(e) {
    console.log('switch2 发生 change 事件，携带值为', e.detail.value)
  },
  bindNewPublish() {
    const index = this.data.tabIndex
    wx.navigateTo({
      url: `../../pages/publish/index?&index=${index}`
    })
  },
  bindRecords() {
    wx.navigateTo({
      url: '../../vip/records/index'
    })
  },
  bindCompile(e) {
    const index = this.data.tabIndex
    const {
      id
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../../vip/compile/index?index=${index}&id=${id}`
    })
  },
  bindPayDetail(e) {
    const {
      id
    } = e.currentTarget.dataset
    const {
      tabIndex
    } = this.data
    const path = 'mypublish'
    wx.navigateTo({
      url: `../../vip/payDeatil/index?id=${id}&index=${tabIndex}&from=${path}`
    })
  },
  bindTabChangeIndex(e) {
    const index = e.detail
    this.setData({
      tabIndex: index
    })
  },
  bindDelete(e) {
    const _this = this
    const {
      state,
      id,
      index
    } = e.currentTarget.dataset
    const {
      rent,
      sell,
      maintain,
      tabIndex
    } = this.data
    wx.showModal({
      title: '温馨提示',
      content: '是否删除',
      success(res) {
        if (res.confirm) {
          if (tabIndex === 0) {
            app.$api.publish.rentDelete({
              id
            }).then((e) => {
              toast('删除成功', 1000)
              rent.splice(index, 1)
              _this.setData({
                rent: rent
              })
            }).catch((e) => {
              toast('请稍后再试', 1000)
            })
          } else if (tabIndex === 1) {
            app.$api.publish.saleDelete({
              id
            }).then((e) => {
              toast('删除成功', 1000)
              sell.splice(index, 1)
              _this.setData({
                sell: sell
              })
            }).catch((e) => {
              toast('请稍后再试', 1000)
            })
          } else {
            app.$api.publish.maintainDelete({
              id
            }).then((e) => {
              toast('删除成功', 1000)
              maintain.splice(index, 1)
              _this.setData({
                maintain: maintain
              })
            }).catch((e) => {
              toast('请稍后再试', 1000)
            })
          }
        } else if (res.cancel) {
          return
        }
      }
    })
  },
  bindDetail(e) {
    const {
      id
    } = e.currentTarget.dataset
    const {
      tabIndex
    } = this.data
    wx.navigateTo({
      url: `/pages/detail/index?id=${id}&tabIndex=${tabIndex}`
    })
  },
  bindNonPayDetail(e) {
    const {
      id
    } = e.currentTarget.dataset
    app.$api.user.againPay({
      infoid:id
    }).then((res) => {
      const data = res.data
      wx.requestPayment({
        timeStamp: data.timestamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.sign,
        success(data) {
          wx.navigateTo({
            url: '/personal/person/index',
          })
        },
        fail(data) {}
      })
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  }
})